﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using bldrdb;
//using System.Query;
//using System.Expressions;
using ActiveRecord;
namespace poco
{

    class rec
    {
      public int Id;
      public  string f1;
      public  int f2;
      public string SameFieldName;
    }

    class childrec
    {
        public int ParentId;
        public int f3;
        public string SameFieldName;
    }

    class Program
    {
        static void Main(string[] args)
        {
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["local"].ConnectionString;
            bldrdb.MainDB.instance().SetOptions(connString);

            ActiveRecordTests.basic();
            ActiveRecordTests.dataTypes();
            ActiveRecordTests.expressions();
            ActiveRecordTests.master_and_detail();
            ActiveRecordTests.left_join();
            ActiveRecordTests.sort();
            ActiveRecordTests.plain_sql_support();
            ActiveRecordTests.custom_ddl();
            return;

          
           

            
                
           //string connString = System.Configuration.ConfigurationManager.ConnectionStrings["local"].ConnectionString;
           //bldrdb.MainDB.instance().SetOptions(connString);
           var sp = bldrdb.DBFactory.Create();

          
           string createSQL = SqlConstructor<rec>.SQLCreateTable();
           sp.SetSQL(createSQL);
           sp.Execute();


           
         
            //
           var ormChild = new DbMapper<childrec>();
           ormChild.CreateTable();
           ormChild.Insert(new childrec { ParentId = 1, f3 = 1 , SameFieldName="c1"});
           ormChild.Insert(new childrec { ParentId = 1, f3 = 2, SameFieldName = "c2" });
           ormChild.Insert(new childrec { ParentId = 2, f3 = 3, SameFieldName = "c3" });
           var rs3 = ormChild.Where(r => r.ParentId==1);


           var ormRec = new DbMapper<rec>();

           ormRec.Insert(new rec() { Id = 1, f2 = 44, SameFieldName = "p1" });
           ormRec.Insert(new rec() { Id = 2, f2 = 44, SameFieldName = "p2" });
           ormRec.Update(new { f2 = 45 }, (r)=>r.f2==44);
           ormRec.Delete((r) => r.f2 == 44);
           rec rf10=ormRec.First((r) => r.f2 == 10);
           var rs2 = ormRec.All();

           var rs3a = ormRec.Where(rec => rec.f2 != 10);

           ormRec.Join<childrec>((p,c)=>p.Id==c.ParentId);


           var joined1=ormRec.Join<childrec>((p, c) => p.Id == c.ParentId).Where((p,c)=>p.f2==45);

           ormChild.Join<rec>((c, p) => p.Id == c.ParentId);
        }
    }
}
